<?php
class MyFunctions{
    
    public static function get_content_by_url($url){
        $content = file_get_contents($url);
        do{
            $content = str_replace("  "," ",$content);
        }while(strpos($content,"  ",0)!==false);
        return $content;
    }
    
   
    public static function get_content_by_tag($content, $tag_and_more,$include_tag = true){
        $p = stripos($content,$tag_and_more,0);
        
        if($p===false) return "";
        $content=substr($content,$p);
        $p = stripos($content," ",0);
        if(abs($p)==0) return "";
        $open_tag = substr($content,0,$p);
        $close_tag = substr($open_tag,0,1)."/".substr($open_tag,1).">";
        
        $count_inner_tag = 0;
        $p_open_inner_tag = 1; 
        $p_close_inner_tag = 0;
        $count=1;
        do{
            $p_open_inner_tag = stripos($content,$open_tag,$p_open_inner_tag);
            $p_close_inner_tag = stripos($content,$close_tag,$p_close_inner_tag);
            $count++;
            if($p_close_inner_tag!==false) $p = $p_close_inner_tag;
            if($p_open_inner_tag!==false){
                if(abs($p_open_inner_tag)<abs($p_close_inner_tag)){
                    $count_inner_tag++;
                    $p_open_inner_tag++;
                }else{
                    $count_inner_tag--;
                    $p_close_inner_tag++;
                }
            }else{
                $count_inner_tag--;
                if($p_close_inner_tag>0) $p_close_inner_tag++;
            }
        }while($count_inner_tag>0);
        if($include_tag)
            return substr($content,0,$p+strlen($close_tag));
        else{
            $content = substr($content,0,$p);
            $p = stripos($content,">",0);
            return substr($content,$p+1);
        }
    }
   
    public static function fix_src_img_tag($content, $url){
        $p_start = 0;
        $start_tag = "<img";
        $loop = true;
        $double_ = true;
        if(substr($url,strlen($url)-1,1)=="/") $url = substr($url,0,strlen($url)-1);
        $src = "src=";
        $content=str_ireplace("src =",$src,$content);
        $content=str_ireplace("src= ",$src,$content);
        $len=0;
        do{
            $p_start = stripos($content,$start_tag,$p_start);
            $len=0;
            if($p_start!==false){
                $p_start=stripos($content,$src,$p_start+1);
                if($p_start>0){
                    $t = substr($content,strlen($src)+$p_start,1);
                    if($t=="\"" || $t=="'"){
                        $p_start += strlen($src)+1;
                    }else{
                        $p_start += strlen($src);
                    }
                    $content = substr($content,0,$p_start).$url.substr($content,$p_start);
                }
                $p_start+=$len+1;
            }else{
                $loop=false;
            }
        }while($loop);
        return $content;
    }
    
   
    public static function list_all_link($content, $url, $attribute = "class", $remove_image_link=true){
        $list = array();
        $bool = true;
        $i=0;
        $href="";
        $title="";
        $attr = "";
        $content = str_ireplace("href =","href=",$content);
        $content = str_ireplace("href= ","href=",$content); 
        $content = str_ireplace($attribute." =",$attribute."=",$content);
        $content = str_ireplace($attribute."= ",$attribute."=",$content);//$sttribute
        do{
            $p_start = 0;
            $p_end = 0;
            $p_start = strpos($content,"<a",$p_start);
            if($p_start!==false){
                $p_end = strpos($content,"</a>",$p_start);
                if($p_end>0){
                    $temp = substr($content,$p_start,$p_end-$p_start);
                    $content = substr($content,$p_end+strlen("</a>"));
                    $p_start = strpos($temp,"href=",0);
                    if($p_start>0){
                        $attr = $temp;
                        $temp=trim(substr($temp,$p_start+strlen("href=")));
                        $t= substr($temp,0,1);
                        if(($t=="\"") || ($t=="'")){
                            $p_end = strpos($temp,$t,1);
                            $href = substr($temp,1,$p_end-1);    
                        }else{
                            $p_start=strpos($temp," ",0);
                            $p_end=strpos($temp,">",0);
                            
                            if($p_start>0 && ($p_start<$p_end)){
                                $href=substr($temp,0,$p_start);
                            }else{
                                $href=substr($temp,0,$p_end);
                            }
                        }
                        $j=$i-1;
                        $p_end = strpos($temp,">",0);
                        $title=substr($temp,$p_end+1);
                        if($remove_image_link){
                            if(strpos($title,"<img",0)===false){
                                $j++;
                            }
                        }else{
                            $j++;
                        }
                        if($j==$i){
                            if(substr($href,0,1)=="/"){
                                $href=$url.$href; 
                            }  
                            $p_start = stripos($attr,$attribute."=",0);
                            if($p_start!==false){
                                $attr = substr($attr,$p_start+strlen($attribute."="));
                                $t = substr($attr,0,1);
                                if(($t=="\"") || ($t=="'")){
                                    $p_end = strpos($attr,$t,1);
                                    $attr = substr($attr,1,$p_end-1);
                                }else{
                                    $p_start = strpos($attr," ",0);
                                    $p_end = strpos($attr,">",0);
                                    if($p_star!==false){
                                        if($p_end===false || $p_end > $p_start)
                                            $attr = substr($attr,0,$p_start);
                                        else
                                            $attr = substr($attr,0,$p_end);
                                    }else if($p_end!==false){
                                        $attr = substr($attr,0,$p_end);
                                    }else{
                                        $attr = "";
                                    }
                                }
                            }else{
                                $attr = "";
                            }
                            $title=trim(str_replace("&nbsp;"," ",$title));
                            $list[$j]['href']=$href;
                            $list[$j]['title']=trim($title);
                            $list[$j][$attribute]=$attr;
                            $i++;
                        }
                    }
                }
            }else{
                $bool=false;
            }
        }while($bool);
        return $list;
    }
}
?> 